
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
	/// <summary>
	/// Right_MemberRole Data Class
	/// </summary>
	public partial class Right_MemberRoleDAL : BaseDAL
	{
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
		public string TableName = "Right_MemberRole";

		public Right_MemberRoleDAL()
		{}

		#region  成员方法
		/// <summary>
		///  新增一条数据
		/// </summary>
		public bool Add(Right_MemberRole model)
		{
			StringBuilder strSql = new StringBuilder();
			
			strSql.Append("insert into [Right_MemberRole](");
			strSql.Append("[MemberAccount],[RoleID],[Type]");
			strSql.Append(") values (");
			strSql.Append("@MemberAccount,@RoleID,@Type");
			strSql.Append(")");
			
			SqlParameter[] parameters = {
				  
					new SqlParameter("@MemberAccount", model.MemberAccount),
					new SqlParameter("@Type", (int)model.Type),
					new SqlParameter("@RoleID", model.RoleID)
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        public bool Add(string MemberAccountS, Guid role)
        {
            SqlParameter[] parms = new SqlParameter[] {
				new SqlParameter("MemberAccountS", MemberAccountS),
                new SqlParameter("role",role)
			};
            int rows = 0;
            DbHelperSQL.RunProcedure("up_AddRoleRightMember", parms, out rows);
            return rows != -1;
        }

		/// <summary>
		///  保存一条数据
		/// </summary>
		public bool Update(Right_MemberRole model)
		{
			StringBuilder strSql = new StringBuilder();
			
			strSql.Append("update [Right_MemberRole] set ");
			strSql.Append("[RoleID]=@RoleID");
			strSql.Append(" where [MemberAccount]=@MemberAccount and [Type]=@Type");
			
			SqlParameter[] parameters = {
				  
					new SqlParameter("@MemberAccount", model.MemberAccount),
					new SqlParameter("@Type", (int)model.Type),
					new SqlParameter("@RoleID", model.RoleID)
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		///</summary>
		public bool Delete(String MemberAccount, SiteType type)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from [Right_MemberRole] ");
			strSql.Append(" where [MemberAccount]=@MemberAccount and [Type]=@Type");
			
			SqlParameter[] parameters = {
			
				new SqlParameter("@MemberAccount", MemberAccount),
				new SqlParameter("@Type", (int)type),
			  
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Right_MemberRole Get(String MemberAccount, SiteType type)
		{
			StringBuilder strSql = new StringBuilder();
			
			strSql.Append("select  top 1 * from [Right_MemberRole] ");
			strSql.Append(" where [MemberAccount]=@MemberAccount and [Type]=@Type");
			
			SqlParameter[] parameters = {
			
				new SqlParameter("@MemberAccount", MemberAccount)     ,
				new SqlParameter("@Type", (int)type),
			  
			};

			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 检查对象是否存在
		/// </summary>
		public bool Exists(Guid roleID)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("select  1  from [Right_MemberRole] ");
			strSql.Append(" where [RoleID]=@Role");

			SqlParameter[] parameters = {
			
				new SqlParameter("Role", roleID)             
			  
			};

			return DbHelperSQL.Exists(strSql.ToString(), parameters);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Right_MemberRole Get(DataRow row)
		{
			if (row == null) return null;

			Right_MemberRole model = new Right_MemberRole();
	
			
			if (row["MemberAccount"] != DBNull.Value)
			{
				model.MemberAccount = (String)row["MemberAccount"];
			}      
			
			if (row["RoleID"] != DBNull.Value)
			{
				model.RoleID = (Guid)row["RoleID"];
			}

			if (row["Type"] != DBNull.Value)
			{
				model.Type = (SiteType)row["Type"];
			}    

			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet Gets(string sqlWhere)
		{
			StringBuilder strSql = new StringBuilder();
			
			strSql.Append("select * from [Right_MemberRole] ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}
			
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet Gets(int Top, string sqlWhere, string filedOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select ");
			if (Top > 0)
			{
				strSql.Append(" top " + Top.ToString());
			}
			strSql.Append(" *  FROM [Right_MemberRole] ");
			
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}
			if(filedOrder.Trim() != "")
			{
				strSql.Append(" order by " + filedOrder);
			}
			
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string sqlWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) FROM [Right_MemberRole] ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 获得数据列表分页
		/// </summary>
		public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append(" SELECT * FROM [Right_MemberRole]  ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}

			int count = 0;
			DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by MemberAccount desc ", out count, cmdParms);
			total = count;
			return dt;
		}
	  #endregion  成员方法

		public DataTable GetRoleMemberList(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
		{
			string sql = @"SELECT a.*,b.ParentMemberAccount, Name = c.FirstName + ' ' + c.LastName ,c.MemberID
							FROM Right_MemberRole a
							join MemberInfo b on a.MemberAccount=b.MemberAccount
							join MemberBasicInfo c on a.MemberAccount=c.MemberAccount  ";
			if (sqlWhere.Trim() != "")
			{
				sql += " where " + sqlWhere;
			}

			return DbHelperSQL.GetPager(pageindex, pagesize, sql, "order by MemberAccount desc ", out total, cmdParms);
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string MemberAccount, Guid RoleID)
		{
			string sql = @"select count(1) 
							from Right_MemberRole 
							where MemberAccount=@MemberAccount and RoleID=@RoleID";

			SqlParameter[] parameters = {
			
				new SqlParameter("@MemberAccount", MemberAccount),  
				new SqlParameter("@RoleID", RoleID)  
			};

			return DbHelperSQL.Exists(sql, parameters);
		}

		/// <summary>
		/// 删除一条数据
		///</summary>
		public bool Delete(string MemberAccount, Guid RoleID)
		{
			string sql = @"delete from Right_MemberRole 
							where MemberAccount=@MemberAccount and RoleID=@RoleID";

			SqlParameter[] parameters = {			
				new SqlParameter("@MemberAccount", MemberAccount),  
				new SqlParameter("@RoleID", RoleID)
			};


			int rows = DbHelperSQL.ExecuteSql(sql, parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}


  